/*!
 * \file FrReportOpt_get_field.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>
#include "frcomm.h"
#include "frmem.h"
#include "frct_comm.h"
#include "conntrack.h"
#include "frreport_comm.h"
#include "frreport_dbg.h"
#include "frreport_opt.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    Frreport_body_data *p_st_body_data=NULL;
    Conntrack *p_st_ct = NULL;
    char c_tmp = '\0';
    Frvar_field *p_ret = NULL;
    Frreport_data *p_st_report_data = NULL;
    int i_ret = 0;

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_ct->st_l7_analyzer.p_st_l7_module = (L7_module *)&c_tmp;

    p_st_body_data = frreport_body_new_l7(p_st_ct);

    i_ret = frreport_body_l7_add_str(p_st_body_data, (char *)"key1", (char *)"value1");
    i_ret = frreport_body_l7_add_str(p_st_body_data, (char *)"key2", (char *)"value2");
    i_ret = frreport_body_l7_add_str(p_st_body_data, (char *)"key1", (char *)"value3");

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_REQ);

    frreport_data_add_body(p_st_report_data, p_st_body_data);

    p_ret = FrReportOpt_get_field(p_st_report_data, (char *)"key1", 0);

    CU_ASSERT_PTR_NOT_NULL(p_ret);

    frfree(p_st_ct);
    frreport_data_free(&p_st_report_data);

    return;
}

static void __case2(void)
{
    Frreport_body_data *p_st_body_data=NULL;
    Conntrack *p_st_ct = NULL;
    char c_tmp = '\0';
    Frvar_field *p_ret = NULL;
    Frreport_data *p_st_report_data = NULL;
    int i_ret = 0;

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_ct->st_l7_analyzer.p_st_l7_module = (L7_module *)&c_tmp;

    p_st_body_data = frreport_body_new_l7(p_st_ct);

    i_ret = frreport_body_l7_add_str(p_st_body_data, (char *)"key1", (char *)"value1");
    i_ret = frreport_body_l7_add_str(p_st_body_data, (char *)"key2", (char *)"value2");
    i_ret = frreport_body_l7_add_str(p_st_body_data, (char *)"key1", (char *)"value3");

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_REQ);

    frreport_data_add_body(p_st_report_data, p_st_body_data);

    p_ret = FrReportOpt_get_field(p_st_report_data, (char *)"key1", 1);

    CU_ASSERT_PTR_NOT_NULL(p_ret);

    frfree(p_st_ct);
    frreport_data_free(&p_st_report_data);

    return;
}

static void __case3(void)
{
    Frreport_body_data *p_st_body_data=NULL;
    Conntrack *p_st_ct = NULL;
    char c_tmp = '\0';
    Frvar_field *p_ret = NULL;
    Frreport_data *p_st_report_data = NULL;
    int i_ret = 0;

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_ct->st_l7_analyzer.p_st_l7_module = (L7_module *)&c_tmp;

    p_st_body_data = frreport_body_new_l7(p_st_ct);

    i_ret = frreport_body_l7_add_str(p_st_body_data, (char *)"key1", (char *)"value1");
    i_ret = frreport_body_l7_add_str(p_st_body_data, (char *)"key2", (char *)"value2");
    i_ret = frreport_body_l7_add_str(p_st_body_data, (char *)"key1", (char *)"value3");

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_REQ);

    frreport_data_add_body(p_st_report_data, p_st_body_data);

    p_ret = FrReportOpt_get_field(p_st_report_data, (char *)"key1", 2);

    CU_ASSERT_PTR_NULL(p_ret);

    frfree(p_st_ct);
    frreport_data_free(&p_st_report_data);

    return;
}

static void __case4(void)
{
    Frreport_body_data *p_st_body_data=NULL;
    Conntrack *p_st_ct = NULL;
    char c_tmp = '\0';
    Frvar_field *p_ret = NULL;
    Frreport_data *p_st_report_data = NULL;
    int i_ret = 0;

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_ct->st_l7_analyzer.p_st_l7_module = (L7_module *)&c_tmp;

    p_st_body_data = frreport_body_new_l7(p_st_ct);

    i_ret = frreport_body_l7_add_str(p_st_body_data, (char *)"key1", (char *)"value1");
    i_ret = frreport_body_l7_add_str(p_st_body_data, (char *)"key2", (char *)"value2");
    i_ret = frreport_body_l7_add_str(p_st_body_data, (char *)"key1", (char *)"value3");

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_REQ);

    frreport_data_add_body(p_st_report_data, p_st_body_data);

    p_ret = FrReportOpt_get_field(p_st_report_data, (char *)"key3", 0);

    CU_ASSERT_PTR_NULL(p_ret);

    frfree(p_st_ct);
    frreport_data_free(&p_st_report_data);

    return;
}

int add_suite_FrReportOpt_get_field(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frtds_convert FrReportOpt_get_field()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, (char *)"case1", __case1);
    CU_add_test(pSuite, (char *)"case2", __case2);
    CU_add_test(pSuite, (char *)"case3", __case3);
    CU_add_test(pSuite, (char *)"case4", __case4);


    return FR_SUCC;
}
